package com.me.darkness_is_coming.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.Align;

public class RulesContent extends Actor
{
    private Skin skin;
    private Label[] stroke_label;
    private Label[] label;
    private String[] text;
    TextureAtlas food_atlas;
    TextureRegion[] food;
    TextureAtlas cloud_atlas = new TextureAtlas("data/Clouds/clouds.atlas");
    TextureRegion[] cloud;
    float frameWidth;
    float frameHeight;
    float[] deltaY;

    public RulesContent(float x, float y, float width, float height, float frameWidth, float frameHeight)
    {
        setSize(width, height);
        setPosition(x, y);
        System.out.println("pos:" + getX() + " " + getY());
        this.frameWidth = frameWidth;
        this.frameHeight = frameHeight;

        food_atlas = new TextureAtlas("data/Food/food.atlas");
        food = new TextureRegion[5];
        food[0] = new TextureRegion(food_atlas.findRegion("AppleGreen"));
        food[1] = new TextureRegion(food_atlas.findRegion("AppleRed"));
        food[2] = new TextureRegion(food_atlas.findRegion("AppleViolet"));
        food[3] = new TextureRegion(food_atlas.findRegion("AppleRainbow"));
        food[4] = new TextureRegion(food_atlas.findRegion("MushroomFrozen"));

        cloud_atlas = new TextureAtlas("data/Clouds/clouds.atlas");
        cloud = new TextureRegion[4];
        cloud[0] = new TextureRegion(cloud_atlas.findRegion("GreenCloud01"));
        cloud[1] = new TextureRegion(cloud_atlas.findRegion("RedCloud01"));
        cloud[2] = new TextureRegion(cloud_atlas.findRegion("VioletCloud01"));
        cloud[3] = new TextureRegion(cloud_atlas.findRegion("WhiteCloud01"));

        skin = new Skin(Gdx.files.internal("data/skins/uiskin.json"));
        text = new String[18];
        stroke_label = new Label[text.length];
        label = new Label[text.length];

        text[0] = "Welcome to game \"Darkness is coming\"!";
        text[1] = "In this game you control the dragon and need slip away from darkness which try kill your dragon. " +
                "The player can keep track of the distance to the darkness using the scale at the top of the screen. " +
                "Сlouds, which appearing on the dragon's path, will hinder escape from the darkness. " +
                "There are 4 kinds of clouds.";
        text[2] = "Clouds";
        text[3] = "- Only green dragon can can fly through this kind of clouds without slowing.";
        text[4] = "- Only red dragon can can fly through this kind of clouds without slowing.";
        text[5] = "- Only violet dragon can can fly through this kind of clouds without slowing.";
        text[6] = "- These clouds can slow darkness when they encounter with darkness.";
        text[7] = "As you understand if the dragon flies through the cloud which hasn't his color he will be slowed. " +
                "White clouds are special, and appear only in certain circumstances described below.";
        text[8] = "Clouds are not the only problem for a player. " +
                "Darkness will accelerate all time so so you'll need somehow slow darkness to survive. " +
                "For this in the game exist apples and frozen mushrooms.";
        text[9] = "Apples";
        text[10] = "- Dragon can turn green clouds in white clouds.";
        text[11] = "- Dragon can turn red clouds in white clouds.";
        text[12] = "- Dragon can turn violet clouds in white clouds.";
        text[13] = "- This apple give you 1 green, 1 red and 1 violet apple.";
        text[14] = "You can use apples to create a white clouds. " +
                "You can collect apples of one type to create a more powerful clouds (maximum 5 apples of one type). " +
                "Remember, the more apples of one type used to create the cloud the more its retarding force.";
        text[15] = "Frozen mushroom";
        text[16] = "- When you eat frozen mushroom darkness will stopped on several seconds.";
        text[17] = "You learned the basic rules of the game and can now develop your own strategies to save your dragon from the darkness. " +
                "Good luck!";

        deltaY = new float[text.length];
        deltaY[0] = 0.02f; deltaY[1] = 0.11f; deltaY[2] = 0.13f;
        deltaY[3] = 0.03f; deltaY[4] = 0.037f; deltaY[5] = 0.037f;
        deltaY[6] = 0.037f; deltaY[7] = 0.09f; deltaY[8] = 0.11f;
        deltaY[9] = 0.08f; deltaY[10] = 0.012f; deltaY[11] = 0.015f;
        deltaY[12] = 0.015f; deltaY[13] = 0.035f; deltaY[14] = 0.12f;
        deltaY[15] = 0.08f; deltaY[16] = 0.04f; deltaY[17] = 0.07f;

        for (int i = 0; i < label.length; i++)
        {
            if ((i == 0) || (i == 2) || (i == 9) || (i == 15))
                label[i] = new Label(text[i], skin, "orange-text");
            else
                label[i] = new Label(text[i], skin, "yellow-text");
            label[i].setFontScale(Gdx.graphics.getHeight() / 1216f);
            label[i].setAlignment(Align.left);
            label[i].setWrap(true);
            stroke_label[i] = new Label(text[i], skin, "brown-text");
            stroke_label[i].setFontScale(Gdx.graphics.getHeight() / 1216f);
            stroke_label[i].setAlignment(Align.left);
            stroke_label[i].setWrap(true);
        }

        label[0].setFontScale(1.6f);
        label[2].setFontScale(1.4f);
        label[9].setFontScale(1.4f);
        label[15].setFontScale(1.4f);
        stroke_label[0].setFontScale(1.6f);
        stroke_label[2].setFontScale(1.4f);
        stroke_label[9].setFontScale(1.4f);
        stroke_label[15].setFontScale(1.4f);

        setLabelsSize();
        setLabelsPosition();
    }

    @Override
    public void draw(Batch batch, float parentAlpha)
    {
        batch.setColor(getColor());
        batch.draw(cloud[0], Gdx.graphics.getWidth() * 0.01f, getY() + Gdx.graphics.getHeight() * 1.53f, cloud[0].getRegionWidth() * 0.8f * 1.6f, cloud[0].getRegionHeight() * 1.6f);
        batch.draw(cloud[1], Gdx.graphics.getWidth() * 0.01f, getY() + Gdx.graphics.getHeight() * 1.445f, cloud[0].getRegionWidth() * 0.8f * 1.6f, cloud[0].getRegionHeight() * 1.6f);
        batch.draw(cloud[2], Gdx.graphics.getWidth() * 0.01f, getY() + Gdx.graphics.getHeight() * 1.36f, cloud[0].getRegionWidth() * 0.8f * 1.6f, cloud[0].getRegionHeight() * 1.6f);
        batch.draw(cloud[3], Gdx.graphics.getWidth() * 0.01f, getY() + Gdx.graphics.getHeight() * 1.275f, cloud[0].getRegionWidth() * 0.8f * 1.6f, cloud[0].getRegionHeight() * 1.6f);
        batch.draw(food[0], Gdx.graphics.getWidth() * 0.05f, getY() + Gdx.graphics.getHeight() * 0.77f, food[1].getRegionWidth() * 1.6f, food[1].getRegionHeight() * 1.6f);
        batch.draw(food[1], Gdx.graphics.getWidth() * 0.05f, getY() + Gdx.graphics.getHeight() * 0.70f, food[1].getRegionWidth() * 1.6f, food[1].getRegionHeight() * 1.6f);
        batch.draw(food[2], Gdx.graphics.getWidth() * 0.05f, getY() + Gdx.graphics.getHeight() * 0.63f, food[1].getRegionWidth() * 1.6f, food[1].getRegionHeight() * 1.6f);
        batch.draw(food[3], Gdx.graphics.getWidth() * 0.05f, getY() + Gdx.graphics.getHeight() * 0.56f, food[1].getRegionWidth() * 1.6f, food[1].getRegionHeight() * 1.6f);
        batch.draw(food[4], Gdx.graphics.getWidth() * 0.05f, getY() + Gdx.graphics.getHeight() * 0.15f, food[1].getRegionWidth() * 1.6f, food[1].getRegionHeight() * 1.6f);
        setLabelsPosition();
        for (int i = 0; i < label.length; i++)
        {
            stroke_label[i].draw(batch, parentAlpha);
            label[i].draw(batch, parentAlpha);
        }
    }

    private void setLabelsSize()
    {
        stroke_label[0].setSize(getWidth(), Gdx.graphics.getHeight() * 0.03f);
        stroke_label[0].setAlignment(Align.center);
        label[0].setSize(getWidth(), Gdx.graphics.getHeight() * 0.03f);
        label[0].setAlignment(Align.center);

        stroke_label[1].setSize(getWidth(), Gdx.graphics.getHeight() * 0.08f);
        label[1].setSize(getWidth(), Gdx.graphics.getHeight() * 0.08f);

        stroke_label[2].setSize(getWidth(), Gdx.graphics.getHeight() * 0.025f);
        stroke_label[2].setAlignment(Align.center);
        label[2].setSize(getWidth(), Gdx.graphics.getHeight() * 0.025f);
        label[2].setAlignment(Align.center);

        stroke_label[3].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[3].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[4].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[4].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[5].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[5].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[6].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[6].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[7].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        label[7].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);

        stroke_label[8].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        label[8].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);

        stroke_label[9].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        stroke_label[9].setAlignment(Align.center);
        label[9].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        label[9].setAlignment(Align.center);

        stroke_label[10].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[10].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[11].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[11].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[12].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[12].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[13].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[13].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[14].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        label[14].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);

        stroke_label[15].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        stroke_label[15].setAlignment(Align.center);
        label[15].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        label[15].setAlignment(Align.center);

        stroke_label[16].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);
        label[16].setSize(getWidth() * 0.7f, Gdx.graphics.getHeight() * 0.05f);

        stroke_label[17].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
        label[17].setSize(getWidth(), Gdx.graphics.getHeight() * 0.05f);
    }

    private void setLabelsPosition()
    {
        for (int i = 0; i < label.length; i++)
        {
            stroke_label[i].setPosition(0, getY() + Gdx.graphics.getHeight() * (2.0f - deltaY[i]) - stroke_label[i].getHeight() - Gdx.graphics.getHeight() * 0.0013f); // - W * 0.0021f; - H * 0.0012f
            label[i].setPosition(Gdx.graphics.getWidth() * 0.0021f, getY() + Gdx.graphics.getHeight() * (2.0f - deltaY[i]) - label[i].getHeight());
            for (int j = 0; j < i; j++)
            {
                stroke_label[i].setY(stroke_label[i].getY() - stroke_label[j].getHeight() - Gdx.graphics.getHeight() * deltaY[j]);
                label[i].setY(label[i].getY() - label[j].getHeight() - Gdx.graphics.getHeight() * deltaY[j]);
            }
        }
        stroke_label[3].setX(Gdx.graphics.getWidth() * 0.2221f);
        label[3].setX(Gdx.graphics.getWidth() * 0.22f);
        stroke_label[4].setX(Gdx.graphics.getWidth() * 0.2221f);
        label[4].setX(Gdx.graphics.getWidth() * 0.22f);
        stroke_label[5].setX(Gdx.graphics.getWidth() * 0.2221f);
        label[5].setX(Gdx.graphics.getWidth() * 0.22f);
        stroke_label[6].setX(Gdx.graphics.getWidth() * 0.2221f);
        label[6].setX(Gdx.graphics.getWidth() * 0.22f);

        stroke_label[10].setX(Gdx.graphics.getWidth() * 0.2021f);
        label[10].setX(Gdx.graphics.getWidth() * 0.20f);
        stroke_label[11].setX(Gdx.graphics.getWidth() * 0.2021f);
        label[11].setX(Gdx.graphics.getWidth() * 0.20f);
        stroke_label[12].setX(Gdx.graphics.getWidth() * 0.2021f);
        label[12].setX(Gdx.graphics.getWidth() * 0.20f);
        stroke_label[13].setX(Gdx.graphics.getWidth() * 0.2021f);
        label[13].setX(Gdx.graphics.getWidth() * 0.20f);

        stroke_label[16].setX(Gdx.graphics.getWidth() * 0.2021f);
        label[16].setX(Gdx.graphics.getWidth() * 0.20f);
    }

    public void dispose()
    {
        food_atlas.dispose();
        cloud_atlas.dispose();
    }
}
